﻿<%@ page language="java" import="java.text.*,java.util.*,ct.zxd.db.*,ct.zxd.bean.*,net.sf.json.JSONArray,net.sf.json.JSONObject" pageEncoding="utf-8"
contentType="text/html"
%>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <title></title>  
    <link rel="stylesheet" href="css/pintuer.css">
    <link rel="stylesheet" href="css/admin.css">	
    <link rel="stylesheet" href="css/traces.css">
    <script src="js/jquery.js"></script>
    <script src="js/pintuer.js"></script>  
    <script src="js/jedate.js"></script>
    <!-- <script src="js/LodopFuncs.js"></script>   -->
<!-- 调用LODOP控件代码-->  
<!-- <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>  
  <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0 pluginspage="install/install_lodop.exe"></embed>  
</object>  --> 
</head>

<style>
.datainp{ width:160px; height:30px; border:1px #ccc solid;text-align:center;}
.selectbox{ width:80px; height:30px; border:1px #ccc solid;text-align:center;}
.buttonright{float:right}
.txtleft{text-align:left;}
</style>

  <%
  Map<String, String> statusMap = new HashMap<String, String>();
  statusMap.put("1", "待付款");statusMap.put("2", "待发货");statusMap.put("0", "全部");
  statusMap.put("3", "已发货");statusMap.put("4", "已完成");statusMap.put("5", "已完成");
  
  String shop = request.getParameter("shopid"); 
  String condition = "&shopid="+shop; //用于发送页码链接中的筛选条件
  String action = "book.jsp?shopid="+shop;
  String scondition = "where shop_id="+shop;
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  
  String selectType="",selectStatus="0",id="",end="";
  String start = request.getParameter("start");
  if(start!=null){
  		/* selectType = request.getParameter("selectType");
  		selectType = selectType==null ? "" : selectType; */
		selectStatus = request.getParameter("selectStatus");
  		if(selectStatus == null) selectStatus = "0";
  		
		//selectStatus = selectStatus==null ? "" : selectStatus;
		/* id = request.getParameter("id");
		id = selectStatus==null ? "" : id; */
		end = request.getParameter("end");
		
		Date startDate = formatter.parse(start);
		long starttime = startDate.getTime();
		Date endDate = formatter.parse(end);
		long endtime = endDate.getTime();
	
		//if(!selectType.equals(""))scondition+=" and type='"+selectType+"'";
		//if(!selectStatus.equals(""))scondition+=" and status='"+selectStatus+"'";
		//if(!id.equals(""))scondition+=" and id="+id;
		
		if(!selectStatus.equals("0")){
			if(selectStatus.equals("4")){  //已完成和已评论 5 都属于已完成状态
				scondition+=" and status >= 4";
			}else{
				scondition+=" and status="+selectStatus;
			}
		}
		
		scondition+=" and time>="+starttime;	
		scondition+=" and time<="+endtime;
		condition+="&start="+start+"&end="+end;
   }else{
   		start = "2021-05-01 08:00:00";
   }	
	//condition+= "&selectType="+selectType+"&selectStatus="+selectStatus+"&id="+id;
	//condition+= "&selectStatus="+selectStatus;

   Dao dao=new Dao();
   //String shopName = dao.fetchShopName(shop);
   int rowNum=dao.getRowNum("ordertable",scondition);//从数据库中查询记录总数 
   int pageSize=10;//页大小   
   int totalPage=1;//总页数
   if(rowNum % pageSize==0){//如果能整除
     totalPage=rowNum / pageSize;
   }else{
     totalPage=rowNum / pageSize+1;
   }
   int displayPage=1;//显示页
   String pages=request.getParameter("pages");//用户选择的显示页
   if(pages!=null){
     	displayPage=Integer.parseInt(pages); 
     	if(displayPage<1)displayPage=1;
     	if(displayPage>totalPage)displayPage=totalPage;    
   }  

   List<Map<String,Object>> list=dao.getDishList(displayPage,pageSize,scondition);//获取数据

   %>
	
<body>
<form method="post" action="<%=action%>">
  <div class="panel admin-panel">
    <div class="panel-head">
	<div class="padding">
	
	<%-- <strong>订单类型</strong>&nbsp
	<select name="selectType" size="1" class="selectbox">
		<option value="<%=selectType %>"><%=selectType %></option>
		<option value=""></option>
		<option value="外送">外送</option>
		<option value="堂食">堂食</option>
	</select>
	&nbsp&nbsp&nbsp<strong>订单状态</strong>&nbsp
	<select name="selectStatus" size="1" class="selectbox">
		<option value="<%=selectStatus %>"><%=selectStatus %></option>
		<option value=""></option>
		<option value="未打印">未打印</option>
		<option value="已打印">已打印</option>
	</select>
	&nbsp&nbsp&nbsp<strong>订单编号</strong>&nbsp
	<input class="datainp txtleft" name="id" type="text" value="<%=id%>"> --%>
	
	</div>
	<div class="padding">
	
		<strong>开始时间</strong>&nbsp
		<input class="datainp" id="startdate" name="start" type="text" value="<%=start%>">
		&nbsp&nbsp&nbsp<strong>结束时间</strong>&nbsp
		<input class="datainp" id="enddate" name="end" type="text" value="<%=end%>">
		
		&nbsp&nbsp&nbsp<strong>订单状态</strong>&nbsp
		<select name="selectStatus" size="1" class="selectbox">
			<option value="<%=selectStatus %>"><%=statusMap.get(selectStatus) %></option>
			<option <%=selectStatus.equals("0") ? "hidden" : "" %> value="0">全部</option>
			<option <%=selectStatus.equals("1") ? "hidden" : "" %> value="1">待付款</option>
			<option <%=selectStatus.equals("2") ? "hidden" : "" %> value="2">待发货</option>
			<option <%=selectStatus.equals("3") ? "hidden" : "" %> value="3">已发货</option>
			<option <%=selectStatus.equals("4") ? "hidden" : "" %> value="4">已完成</option>
		</select>
		
		<button class="button bg-main icon-search buttonright" type="submit"> 查询</button>
	</div>
	
    </div>
    <!--<div class="padding border-bottom">
        <ul class="search">
        <li>
          <button type="button"  class="button border-green" id="checkall"><span class="icon-check"></span> 全选</button>
          <button type="submit" class="button border-red"><span class="icon-trash-o"></span> 批量删除</button> 
        </li>
      </ul> 
    </div>-->
    <table class="table table-hover text-center">         
      <tr>
        <th width="7%">编号</th>
        <th width="8%">状态</th>       
        <th width="10%">时间</th>
        <th width="22%">详情</th>
        <th width="5%">金额</th>
        <th width="20%">地址</th>
        <!-- <th width="5%">类型</th> -->
        <th width="28%">操作</th>       
      </tr>     
      
       <%
       if(list!=null){
       for(Map<String,Object> map : list){
     		
       		String timestr = map.get("time").toString();
       		long time = Long.parseLong(timestr);
   			String dateString = formatter.format(new Date(time)); 
   			
   			
   			String ship = map.get("shipper") == null ? "" : map.get("shipper").toString();
   			String code = map.get("kdcode") == null ? "" : map.get("kdcode").toString();
   			
   			JSONObject jb = JSONObject.fromObject(map.get("list").toString());
        	JSONArray ja = jb.getJSONArray("order");
        	//System.out.println(code+"-- "+ship);
        	
        	/* jb.put("time", dateString);
        	jb.put("id",map.get("id").toString());
        	jb.put("money", map.get("money").toString());
        	jb.put("address", map.get("address").toString());
        	jb.put("type", map.get("type").toString());
        	//jb.put("note", map.get("note").toString());
        	//jb.put("shopname", shopName);
        	jb.put("shopid", shop); */
        	
      %>

        <tr>
          <!-- <td><input type="checkbox" name="id[]" value="1" /> -->
            <td><%=map.get("id").toString() %></td>
          <td><%=statusMap.get(map.get("status").toString())%></td>
          <td><%=dateString%></td>
          <td><%for (int i = 0; i < ja.size(); i++) {%>
          <%=ja.getJSONObject(i).getString("name")%>
          <%="  x"+ja.getJSONObject(i).getString("quantity")%>
          <%="     "+ja.getJSONObject(i).getString("price") %>
          <br>
       	  <%}%></td>  
           <td><%=map.get("money").toString()%></td>         
          <td><%=map.get("address").toString()%></td>
          <%-- <td><%=map.get("type").toString()%></td> --%>
          <td><div class="button-group"> 
          
          <a class="button border-main orderid<%=map.get("id").toString() %>"  onclick="return addorder(<%=map.get("status") %>,<%=map.get("id").toString() %>, <%=shop%>, '<%=ship%>', '<%=code%>')" ><span class="icon-print"></span> 发货</a>
		  <a class="button border-red tracesid<%=map.get("id").toString() %>"  onclick="return traces(<%=map.get("id").toString() %>, '<%=ship%>', '<%=code%>')" ><span class="icon-road"></span> 物流</a> 
          
          
<!-- <a class="button border-main" href="javascript:void(0)" onclick='return print(<%=jb%>)'><span class="icon-print"></span> 打印</a> -->
<%-- <a class="button border-red" href="javascript:void(0)" onclick="return del(<%=map.get("id").toString() %>,<%=shop %>)"><span class="icon-trash-o"></span> 删除</a> --%>  
	
		</div></td>
        </tr>   
         <% } } %>
 
      <tr>
      	<td colspan="8"><div class="pagelist">
      	<%if(totalPage>1 && displayPage>1) {%>
      	<a href="book.jsp?pages=1<%=condition%>">第一页</a>  
      	<a href="book.jsp?pages=<%=displayPage-1%><%=condition%>">上一页</a>
      	<%}%>
   		<% if(totalPage>1 && displayPage<totalPage){%>
    	<a href="book.jsp?pages=<%=displayPage+1%><%=condition%>">下一页</a>
   		<a href="book.jsp?pages=<%=totalPage%><%=condition%>">最后一页</a>
   		<%}%>
   		
   		</div></td>
      </tr>
    </table>
  </div>
</form>


<script type="text/javascript">

    	jeDate({
		dateCell:"#startdate",
		format:"YYYY-MM-DD hh:mm:ss",
		isinitVal:true,
		isTime:true, //isClear:false,
		minDate:"2017-03-01 00:00:00",
	})

	jeDate({
		dateCell:"#enddate",
		format:"YYYY-MM-DD hh:mm:ss",
		isinitVal:true,
		isTime:true, //isClear:false,
		minDate:"2017-03-01 00:00:00",
	})
	
	
function addorder(status, id, shop, shipper, code){	
    		
    if(status == 1){
    	alert("用户尚未付款");
    	return
    }
    		
	order = "[class='button border-main orderid" + id  + "']"; 	
	orderDiv = "[class*='orderid']" + "> div";  /* 所有的表单提示框 */
	orderidDiv = ".orderid" + id +"> div"  ;		/* 点击按钮下的提示框 */
	orderidDivSelect = ".orderid" + id +">div" + " .selectboxtraces"  ;		/* 下拉按钮 */
	orderidDivInput = ".orderid" + id +">div" + " [name='LogisticCode']" ;  /* 订单输入框 */
	orderidDivSub = ".orderid" + id +">div" + " #submit" ;					/* 提交按钮 */
	
	tracesDiv  = "[class*='tracesid']" + "> div";
	
	if($(orderidDiv).length != 0){
		$(orderidDiv).remove();
	}else{
		$(tracesDiv).each(function(){
				$(this).remove();
		});
		$(orderDiv).each(function(){
			$(this).remove();
		});
	 	obj = $("<div><div class=\"from\">"
	 			+ "<select name=\"selectType\"  class=\"selectboxtraces\">"
			 	+ "<option value=\"STO\">申通快递</option>"
			 	+ "<option value=\"YTO\">圆通速递</option>"
			 	+ "<option value=\"HTKY\">百世快递</option>"
			 	+ "<option value=\"HHTT\">天天快递</option></select>"
			 	+ "<input class=\"input-text\" type=\"text\" name=\"LogisticCode\" placeholder=\"请输入订单号\"/>"
			    + "<input id=\"submit\" type=\"button\" value=\"提交\"></div></div>");
		obj.attr("class","t show" + id);	
		$(order).append(obj);	
		shipper == "" ?  $(orderidDivSelect).val("STO"): $(orderidDivSelect).val(shipper); 
		$(orderidDivInput).val(code);
	}	  	
	$(orderidDiv).on("click",function(){
		event.stopPropagation();
	});
	$(orderidDivSub).on("click",function(){
		
		if($(orderidDivInput).val()==""){
			$(orderidDivInput).attr("placeholder","快递单号不能为空!");
			$(orderidDivInput).attr("class",'input-text infoTextarea change');
			setTimeout(function(){
				$(orderidDivInput).attr("placeholder","请输入快递单号");
				$(orderidDivInput).attr("class",'input-text infoTextarea recover')}
				,1000);
		}else{
			$.ajax({
                url: "EditOrder",
                data: 'ID='+id+'&status='+status+'&shop='+shop+'&shipper='+$(orderidDivSelect).val()+'&code='+$(orderidDivInput).val(),//发送后台数据
                dataType: "json", //后台返回数据的类型，不是json，解析失败，因此调用error:function
                type: "get",//请求类型
                success: function (data) {
              		// servlet writer.write 只返回到error，不返回success
                },
                error:function(XMLResponse){
                	alert(XMLResponse.responseText);
                	window.location.reload();
                }
            });    
			$(orderidDiv).remove();
		}		 
		event.stopPropagation();
	});
}	

    	
    	function traces(id, shipper, code){	
    		orderDiv = "[class*='orderid']" + "> div";  /* 所有的表单提示框 */
    		tracesId  = ".tracesid" + id ;
    		tracesDiv  = "[class*='tracesid']" + "> div";
    		tracesidDiv = ".tracesid" + id +"> div"  ;		/* 点击按钮下的提示框 */
    		if($(tracesidDiv).length != 0){
    			$(tracesidDiv).remove();
    	    }else {
    	    	$(orderDiv).each(function(){
    				$(this).remove();			
    			});
    	    	$(tracesDiv).each(function(){
    	    		$(this).remove();
    			});
    //---------------------------------------------------------------------------------------------------------
			    $.ajax({
	                url: "GetLogistics",
	                data: 'shipper='+shipper+'&code='+code,//发送后台数据
	                dataType: "json", //后台返回数据的类型，不是json，解析失败，因此调用error:function
	                type: "get",//请求类型
	                success: function (data) {
	                	if(data.Success && data.Traces.length > 0){
	                		
	    	 		 		div = $("<div></div>");
	    	    	    	div.attr("class","position test-1 show" + id);
	    	    	    	Traces =  $("<div class=\"r-wrap test-1\"></div>") ;
	    	 		 		for(var i =  data.Traces.length - 1 ;i >= 0; i--){
	    	 		 			 TrancesItem = $("<div class=\"r-wrap-item\"></div>") ;
	    	 		 			 /* 左子项 */
	    	 		 			 leftItem =  $("<div class=\"r-item-left \"></div>");
	    	 		 			 leftCrdOne = $("<div class=\"r-item-left-crd \"></div>")
	    	 		 			 point = $("<div class=\"r-point-wrap\"><div class=\"r-point-item\"></div></div>");
	    	 		 			 leftCrdTwo = $("<div class=\"r-item-left-crd\"></div>");
	    	 		 			 /* 右子项 */
	    	 		 		 	 rightItem = $("<div class=\"r-item-right\"></div>");
	    	 		 		 	 rightContent = $("<div class=\"r-item-right-content\"></div>");
	    	 		 		 	 rightTime = $("<div class=\"r-item-right-time\"></div>");
	    	 		 			 if(i == 0){
	    	 		 				 leftCrdTwo.attr("class","r-item-left-crd last");
	    	 		 				TrancesItem.attr("style","padding-bottom:10px")
	    	 		 			 }
	    	 		 			 if(i == (data.Traces.length - 1)){
	    	 		 				leftCrdOne.attr("class","r-item-left-crd first");
	    	 		 				TrancesItem.attr("style","padding-top:10px")
	    	 		 			 }   	 		 			 
	    	 		 			 leftItem.append(leftCrdOne);
	    	 		 			 leftItem.append(point);
	    	 		 			 leftItem.append(leftCrdTwo);
	    	 		 			 rightContent.append(data.Traces[i].AcceptStation);
	    	 		 			 rightTime.append(data.Traces[i].AcceptTime); 
	    	 		 			 rightItem.append(rightContent);
	    	 		 			 rightItem.append(rightTime);	
	    	 		 			 TrancesItem.append(leftItem);
	    	 		 			 TrancesItem.append(rightItem);
	    	 		 			 Traces.append(TrancesItem);
	    	 		 		 }
			    	         div.append(Traces);
			    	         $(tracesId).append(div); 
	    	 		 	}else{
	    	 		 		
	    	 		 		div = $("<div class=\"noTraces\"></div>");
	    	 		 		div.append(data.Reason);
	    	    	    	$(tracesId).append(div);
	    	    	    	}
	                },
	                error:function(XMLResponse){
	                	alert(XMLResponse.responseText);
	                	window.location.reload();
	                }
	            });    
    	    }
   	   			 	 
   	}    	
        	 	
    	
/*
function del(id,shop){
	if(confirm("您确定要删除吗?")){
		 $.ajax({
                url: "EditOrder",
                data: 'ID='+id+'&shop='+shop,//发送后台数据
                dataType: "json", //后台返回数据的类型，不是json，解析失败，可能因此调用error:function
                type: "get",//请求类型
                success: function (data) {
                },
                error:function(XMLResponse){
                	//alert(XMLResponse.responseText);
                	window.location.reload();
                }
            });
	};
}
*/

//-----------------------------------------------------

 	//var LODOP; //声明为全局变量  
 	
        /* function print(json){  
        	$.ajax({
                url: "EditOrder",
                data: 'ID='+json.id+'&shop='+json.shopid+'&flag=9',//发送后台数据
                dataType: "json", //后台返回数据的类型，不是json，解析失败，因此调用error:function
                type: "get",//请求类型
                success: function (data) {
                },
                error:function(XMLResponse){
                	//alert(XMLResponse.responseText);
                	window.location.reload();
                }
            });    
                //创建小票打印页  
        	CreatePrintPage(json);  
        	//打印预览  
        	LODOP.PREVIEW(); 
        	 
       }   */
       
        /* function printOrder(json) {       
	        //创建小票打印页  
	        CreatePrintPage(json);  
	        //开始打印  
	        LODOP.PRINT();  
	          
    	};       */      
          
   /*    function CreatePrintPage(json) {  
        //json 创建模拟服务器响应的订单信息对象  
       
        var hPos=10,//小票上边距  
        pageWidth=580,//小票宽度  
        rowHeight=15;//小票行距  
        //获取控件对象  
        LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));   
        //初始化   
        LODOP.PRINT_INIT("打印");  
        //添加小票标题文本  
        LODOP.ADD_PRINT_TEXT(hPos,90,pageWidth,rowHeight,json.shopname);  
        //上边距往下移  
        hPos+=rowHeight*2;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"订单编号:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.id+"     "+json.type);  
        hPos+=rowHeight; 
         
       if(json.address.length>16){
       		LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth/3,rowHeight*3,"地址:"+json.address);
       		hPos+=rowHeight*4;  
       }else{
       		LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth/3,rowHeight,"地址:"+json.address); 
       		hPos+=rowHeight; 	
       }
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"下单时间:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.time);  
        hPos+=rowHeight*2;     
        LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1);  //画分隔线
        hPos+=5;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"商品名称");  
        LODOP.ADD_PRINT_TEXT(hPos,100,pageWidth,rowHeight,"单价");  
        LODOP.ADD_PRINT_TEXT(hPos,145,pageWidth,rowHeight,"数量");   
        hPos+=rowHeight;  
        //遍历json的商品数组  
        for(var i=0;i<json.order.length;i++){    
            if(json.order[i].name.length<10){  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.order[i].name);  
            }else {  
                //商品名字过长,其他字段需要换行  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.order[i].name);  
                hPos+=rowHeight;  
            }  
            LODOP.ADD_PRINT_TEXT(hPos,100,pageWidth,rowHeight,json.order[i].price);  
            LODOP.ADD_PRINT_TEXT(hPos,145,pageWidth,rowHeight,json.order[i].quantity);  
            hPos+=rowHeight;  
        }  
        //商品遍历打印完毕,空一行  
        hPos+=rowHeight;    
        LODOP.ADD_PRINT_TEXT(hPos,100,pageWidth,rowHeight,"合计: ￥"+json.money);   
        hPos+=rowHeight*2;    
         if(json.note.length>16){
         	LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth/3,rowHeight*4,"备注:"+json.note);  
        	hPos+=rowHeight*4;  
         }else{
         	LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth/3,rowHeight,"备注:"+json.note);  
        	hPos+=rowHeight;  
         }
        LODOP.ADD_PRINT_TEXT(hPos,25,pageWidth,rowHeight,"谢谢惠顾,欢迎下次光临!");  
        //初始化打印页的规格  
        LODOP.SET_PRINT_PAGESIZE(3,pageWidth,45,"");  //第三个参数是最后留下多少空白
          
    }; */    




//-----------------------------------------------------


/*

$("#checkall").click(function(){ 
  $("input[name='id[]']").each(function(){
	  if (this.checked) {
		  this.checked = false;
	  }
	  else {
		  this.checked = true;
	  }
  });
})

function DelSelect(){
	var Checkbox=false;
	 $("input[name='id[]']").each(function(){
	  if (this.checked==true) {		
		Checkbox=true;	
	  }
	});
	if (Checkbox){
		var t=confirm("您确认要删除选中的内容吗？");
		if (t==false) return false; 		
	}
	else{
		alert("请选择您要删除的内容!");
		return false;
	}
}

*/

</script>
</body>
</html>